package com.logos.ruralrevitalization.exception;

import com.logos.ruralrevitalization.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<String> handlerException(Exception e) {
        // 记录详细的异常信息
        log.error("Global exception handler caught an exception", e);

        // 确保Result.fail不会抛出异常
        try {
            return Result.fail(e.getMessage());
        } catch (Exception ex) {
            // 如果Result.fail抛出异常，返回一个默认的错误信息
            log.error("Result.fail failed with exception", ex);
            return Result.fail("An unexpected error occurred");
        }
    }
}
